<!-- 0:年份酒价值 1:数字酒藏认知 2:数字酒藏权益 3:消费场景 4:储运保障 -->
<template>
	<el-dialog v-model="isVisible" :title="row ? titles[row?.updateType] : '新增消费场景'" width="600px" :before-close="handleClose">
		<!-- 消费场景 -->
		<el-form v-if="!row || row?.updateType == '3'" ref="formRef" class="subject-update-form" :model="form" :rules="rules" label-width="100px">
			<el-form-item label="场景标题" prop="contentTitle">
				<el-input v-model="form.contentTitle" :maxlength="10" placeholder="输入场景标题" />
			</el-form-item>
			<el-form-item label="图片" prop="contentImage">
				<uploadImage v-model:file-list="form.contentImage" :limit="1" :fileSize="100 / 1024" packageName="content">
					<template #tip> 图片尺寸要求600*300像素，支持jpg、 png格式，不大于100KB； </template>
				</uploadImage>
			</el-form-item>
			<el-form-item label="排序" prop="order">
				<el-input-number
					@blur="
						() => {
							form.order = form.order || 0;
						}
					"
					class="num-left"
					:min="0"
					:max="999999999"
					:controls="false"
					placeholder="输入排序(默认0，越大越靠前)"
					v-model="form.order"
				/>
			</el-form-item>
			<el-form-item label="关联内容" prop="contentId">
				<RefreshSelect
					v-model="form.contentId"
					:api="fetchCreationList"
					clearable
					:option-map="{ label: (item:any) => `${item.id}(${item.contentTitle})`, value: 'id' }"
					popper-class="refresh-select"
					link="/content/creation"
					placeholder="请选择关联内容"
				></RefreshSelect>
			</el-form-item>
		</el-form>
		<!-- 年份酒价值 -->
		<el-form v-else-if="row?.updateType == '0'" ref="formRef" class="subject-update-form" :model="form" :rules="rules" label-width="100px">
			<el-form-item label="价值标题" prop="contentTitle">
				<el-input v-model="form.contentTitle" :maxlength="10" placeholder="输入价值标题" />
			</el-form-item>
			<el-form-item label="图片" prop="contentImage">
				<uploadImage v-model:file-list="form.contentImage" :limit="1" packageName="content">
					<template #tip> 图片尺寸要求200*200像素，支持jpg、 png格式，不大于100KB； </template>
				</uploadImage>
			</el-form-item>
			<el-form-item label="排序" prop="order">
				<el-input-number
					@blur="
						() => {
							form.order = form.order || 0;
						}
					"
					class="num-left"
					:min="0"
					:max="999999999"
					:controls="false"
					placeholder="输入排序(默认0，越大越靠前)"
					v-model="form.order"
				/>
			</el-form-item>
			<el-form-item label="关联内容" prop="contentId">
				<RefreshSelect
					v-model="form.contentId"
					:api="fetchCreationList"
					clearable
					:option-map="{ label: (item:any) => `${item.id}(${item.contentTitle})`, value: 'id' }"
					popper-class="refresh-select"
					link="/content/creation"
					placeholder="请选择关联内容"
				></RefreshSelect>
			</el-form-item>
		</el-form>
		<!-- 数字酒藏认知 -->
		<el-form v-else-if="row?.updateType == '1'" ref="formRef" class="subject-update-form" :model="form" :rules="rules" label-width="100px">
			<el-form-item label="酒藏标题" prop="contentTitle">
				<el-input v-model="form.contentTitle" :maxlength="10" placeholder="输入酒藏标题" />
			</el-form-item>
			<el-form-item label="酒藏简介" prop="contentExtend">
				<el-input type="textarea" :rows="3" v-model="form.contentExtend" :maxlength="1000" show-word-limit placeholder="输入酒藏简介" />
			</el-form-item>
			<el-form-item label="关联内容" prop="contentId">
				<RefreshSelect
					v-model="form.contentId"
					:api="fetchCreationList"
					clearable
					:option-map="{ label: (item:any) => `${item.id}(${item.contentTitle})`, value: 'id' }"
					popper-class="refresh-select"
					link="/content/creation"
					placeholder="请选择关联内容"
				></RefreshSelect>
			</el-form-item>
		</el-form>
		<!-- 数字酒藏权益 -->
		<el-form v-else-if="row?.updateType == '2'" ref="formRef" class="subject-update-form" :model="form" :rules="rules" label-width="100px">
			<el-form-item label="权益标题" prop="contentTitle">
				<el-input v-model="form.contentTitle" :maxlength="10" placeholder="输入权益标题" />
			</el-form-item>
			<el-form-item label="权益副标题">
				<el-input v-model="form.contentExtend" :rules="rules.contentSubTitle" :maxlength="8" placeholder="输入权益副标题" />
			</el-form-item>
			<el-form-item label="图片" prop="contentImage">
				<uploadImage v-model:file-list="form.contentImage" :limit="1" packageName="content">
					<template #tip> 图片尺寸要求200*200像素，支持jpg、 png格式，不大于100KB； </template>
				</uploadImage>
			</el-form-item>
			<el-form-item label="排序" prop="order">
				<el-input-number
					@blur="
						() => {
							form.order = form.order || 0;
						}
					"
					class="num-left"
					:min="0"
					:max="999999999"
					:controls="false"
					placeholder="输入排序(默认0，越大越靠前)"
					v-model="form.order"
				/>
			</el-form-item>
		</el-form>
		<!-- 储运保障 -->
		<el-form v-else ref="formRef" class="subject-update-form" :model="form" :rules="rules" label-width="100px">
			<el-form-item label="标题" prop="contentTitle">
				<el-input v-model="form.contentTitle" :maxlength="10" placeholder="输入价值标题" />
			</el-form-item>
			<el-form-item label="图片" prop="contentImage">
				<uploadImage v-model:file-list="form.contentImage" :limit="1" packageName="content">
					<template #tip> 图片尺寸要求400*300或300*100像素，支持jpg、 png格式，不大于300KB； </template>
				</uploadImage>
			</el-form-item>
			<el-form-item label="排序" prop="order">
				<el-input-number
					@blur="
						() => {
							form.order = form.order || 0;
						}
					"
					class="num-left"
					:min="0"
					:max="999999999"
					:controls="false"
					placeholder="输入排序(默认0，越大越靠前)"
					v-model="form.order"
				/>
			</el-form-item>
			<el-form-item label="关联内容" prop="contentId">
				<RefreshSelect
					v-model="form.contentId"
					:api="fetchCreationList"
					clearable
					:option-map="{ label: (item:any) => `${item.id}(${item.contentTitle})`, value: 'id' }"
					popper-class="refresh-select"
					link="/content/creation"
					:apiParams="{ status: 2 }"
					placeholder="请选择关联内容"
				></RefreshSelect>
			</el-form-item>
		</el-form>
		<template #footer>
			<span class="dialog-footer">
				<el-button @click="handleClose">取消</el-button>
				<el-button type="primary" v-debounce="submit">保存</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="tsx">
import { FormRules, ElMessage } from 'element-plus';
import { ref, reactive, watch } from 'vue';
import { fetchCreateSubject, fetchUpdateSubject, fetchCreationList } from '/@/api/content';
import RefreshSelect from '/@/components/refreshSelect/index.vue';
import uploadImage from '/@/components/upload/image.vue';

const props = defineProps({
	// 列表内容
	visible: {
		type: Boolean,
		default: () => false,
	},
	row: {
		type: Object,
		default: () => null,
	},
});

const titles = ref<any>({
	0: '编辑年份酒价值',
	1: '编辑数字酒藏认知',
	2: '编辑数字酒藏权益',
	3: '编辑消费场景',
	4: '编辑储运保障',
});

const emit = defineEmits(['handleClose']);

const isVisible = ref(false);

const formRef = ref();
const form = ref<any>({
	order: 0,
});

//校验规则
const rules = reactive<FormRules<any>>({
	contentTitle: [{ required: true, trigger: 'blur', message: '标题不能为空' }],
	contentImage: [{ required: true, trigger: 'blur', message: '图片不能为空' }],
	// contentId: [{ required: true, trigger: 'blur', message: '关联内容不能为空' }],
	contentExtend: [{ required: true, trigger: 'blur', message: '酒藏简介不能为空' }],
	contentSubTitle: [{ required: true, trigger: 'blur', message: '副标题不能为空' }],
});

watch(
	() => props.visible,
	(val) => {
		if (val && props.row) {
			form.value = { order: 0, ...props.row };
		}
		isVisible.value = val;
	},
	{
		immediate: true,
	}
);

const submit = async () => {
	formRef.value.validate(async (valid: boolean) => {
		if (valid) {
			const params = { ...form.value };
			params.contentImage = params.contentImage.map((v: any) => v.url).join();
			params.contentCode = props.row?.updateType;
			const res = props.row.id ? await fetchUpdateSubject(params) : await fetchCreateSubject(params);
			if (res.code == 200) {
				ElMessage.success(props.row ? `${titles.value[props.row?.updateType]}成功` : '新增消费场景成功');
				handleClose(true);
			} else {
				ElMessage.error(res.msg);
			}
		}
	});
};

const handleClose = (success?: boolean) => {
	form.value = {
		order: 0,
	};
	formRef.value.resetFields();
	emit('handleClose', success === true);
};
</script>

<style lang="scss">
.subject-update-form {
	.el-form-item {
		width: 90%;
		.el-input-number {
			width: 100%;
		}
	}
}
</style>
